﻿using ETA.Dtos;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ETA.Service
{
    public class FlowLogService : IFlowLogService
    {


        #region 属性

        private IFlowLogRepository _IFlowLogRepository { set; get; }


        #endregion

        #region 构造函数

        public FlowLogService(IFlowLogRepository IFlowLogRepository)
        {
            _IFlowLogRepository = IFlowLogRepository;
        }

        #endregion


        public CreateFlowLogResponse CreateFlowLog(CreateFlowLogRequest req)
        {
            var res = new CreateFlowLogResponse();
            try
            {
                if (req != null)
                {
                    //if (!IsFlowLogExists(req.FlowLog))
                    //{
                    res.FlowLog = _IFlowLogRepository.Insert(req.FlowLog);
                    res.Result.IsSuccess = true;
                    //}
                    //else
                    //{
                    //    var modifyReq = new ModifyFlowLogRequest
                    //    {
                    //        FlowLog = req.FlowLog
                    //    };
                    //    var modifyRep = ModifyFlowLog(modifyReq);
                    //    if (modifyRep.Result.IsSuccess)
                    //    {
                    //        res.FlowLog = modifyRep.FlowLog;
                    //        res.Result.IsSuccess = true;
                    //    }
                    //}
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
                res.Result.IsSuccess = false;
            }
            return res;
        }

        public ModifyFlowLogResponse ModifyFlowLog(ModifyFlowLogRequest req)
        {
            var res = new ModifyFlowLogResponse();
            try
            {
                if (req != null)
                {
                    _IFlowLogRepository.Merge(req.FlowLog);
                    res.FlowLog = req.FlowLog;
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
                res.Result.IsSuccess = false;
            }
            return res;
        }



        public bool IsFlowLogExists(FlowLog log)
        {
            bool res = false;
            if (log != null)
            {
                if (_IFlowLogRepository.FirstOrDefault
                    (l => l.IsDelete == false && l.RegistrationMainFormId == log.RegistrationMainFormId
                        && l.FlowStepId == log.FlowStepId) != null)
                {
                    res = true;
                }
            }
            return res;
        }


        public QueryFlowLogResponse QueryFlowLog(QueryFlowLogRequest req, out int count)
        {
            count = 0;
            var response = new QueryFlowLogResponse();
            try
            {
                var express = QueryFlogExpress(req);
                var orgList = _IFlowLogRepository.GetAllList(express);
                var flowLogs = FlowLogMap(orgList);
                count = flowLogs.Count;
                response.OrgFlowLogs = orgList;
                response.FlowLogs = flowLogs.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
                response.Result.IsSuccess = true;

            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }

        /// <summary>
        /// dto转换
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<FlowLogDto> FlowLogMap(List<FlowLog> list)
        {
            var res = new List<FlowLogDto>();
            foreach (var item in list)
            {
                var user = item.RegistrationMainForm.User;
                var basicInfos = item.RegistrationMainForm.RegistrationBasicInfos;
                var webMsg = item.WebMessages;
                string sDate = string.Empty;
                string eDate = string.Empty;
                string address = string.Empty;

                if (basicInfos != null && basicInfos.Any())
                {
                    address = basicInfos.First().HomeAddress;
                }

                if (webMsg != null && webMsg.Any())
                {
                    sDate = webMsg.First().SDate.ToString();
                    eDate = webMsg.First().EDate.ToString();
                }
                res.Add(new FlowLogDto
                {
                    Address = address,
                    FlowLogId = item.FlowLogId,
                    FlowStepName = (item.FlowStepInfo.FlowIndex+1)+"-"+item.FlowStepInfo.FlowStepName,
                    HandlerDate = item.HandlerDate == null ? "" : item.HandlerDate.Value.ToString(),
                    Mobile = user.Mobile,
                    State = item.FlowLogStateEnum + "",
                    SubmitDate = item.CreateDate,
                    UserId = user.LoginName,
                    UserName = user.UserName,
                    Sdate = sDate,
                    Edate = eDate,
                    Sex = user.SexEnum.ToString(),
                    RolerName = user.Rolers.First().RolerName
                });
            }
            return res;
        }
        private Expression<Func<FlowLog, bool>> QueryFlogExpress(QueryFlowLogRequest request)
        {
            Expression<Func<FlowLog, bool>> express = log => log.IsDelete == false;
            if (request.FlowLog != null)
            {
                var flowLog = request.FlowLog;
                if (!string.IsNullOrEmpty(request.FlowLog.FlowLogId))
                {
                    express = log => log.FlowLogId == request.FlowLog.FlowLogId && request.FlowLog.IsDelete == false;
                }
                else if (!string.IsNullOrEmpty(flowLog.FlowStepId))
                {
                    express = log => log.FlowStepInfo.FlowStepId == flowLog.FlowStepId;
                }

                if ((!string.IsNullOrEmpty(flowLog.FlowStepId))&&(!string.IsNullOrEmpty(flowLog.RegistrationMainFormId)))
                {
                    express = log => log.FlowStepInfo.FlowStepId == flowLog.FlowStepId&&log.RegistrationMainForm.RegistrationMainFormId==flowLog.RegistrationMainFormId;
                }

            }
            return express;
        }
    }
}
